package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author yb0os1
 * @since 2024-12-27
 */
@Slf4j
//@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {

    private final RabbitMqHelper rabbitMqHelper;

    @Override
    public void likedOrCancel(LikeRecordFormDTO dto) {
        //1、获取用户id
        Long userId = UserContext.getUser();
        boolean flag = dto.getLiked() ? likedHandle(dto, userId) : unlikedHandle(dto, userId);//点赞或者取消点赞是否成功 true代表处理成功 false 失败
 /*       if (dto.getLiked()){
            //2、点赞的处理
            flag = likedHandle(dto,userId);
        }else {
            //3、取消点赞的处理
            flag = unlikedHandle(dto,userId);
        }*/
        if (!flag) {
            log.error("点赞or取消点赞失败");
            return;
        }

        //4、统计点赞数 通过MQ发送出去
        int likedTimes = lambdaQuery()
                .eq(LikedRecord::getBizId, dto.getBizId())
                .count();
        //向MQ发送消息 通知更新点赞数量 发送消息的时候 问答的点赞 笔记的点赞发送不同的队列
        rabbitMqHelper.send(
                MqConstants.Exchange.LIKE_RECORD_EXCHANGE,
                StringUtils.format(MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE, dto.getBizType()),
                LikedTimesDTO.of(dto.getBizId(), likedTimes)
        );
    }

    @Override
    public Set<Long> batchQueryLikedStatus(List<Long> ids) {
        //1、获取当前用户
        Long userId = UserContext.getUser();
        //2、根据bizId和userId查询 点赞状态
        return lambdaQuery().eq(LikedRecord::getUserId, userId)
                .in(LikedRecord::getBizId, ids)
                .list()
                .stream()
                .map(LikedRecord::getBizId)
                .collect(Collectors.toSet());
    }

    @Override
    public void readLikedTimesAndSendMessage(String bizType, int maxBizSize) {

    }

    private boolean unlikedHandle(LikeRecordFormDTO dto, Long userId) {
/*        //1、根据userId和bizId查找唯一的点赞记录
        LikedRecord record = lambdaQuery().eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .one();
        //2、记录为空 代表不存在点赞 不可以取消点赞
        if (record == null) {
            return false;
        }
        //3、记录不为空 取消点赞也就是删除记录
        return removeById(record);*/

        //也可以直接删除 不存在的时候会返回false  存在并且删除成功返回true
        return remove(new LambdaQueryWrapper<LikedRecord>()
                .eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId()));
    }

    private boolean likedHandle(LikeRecordFormDTO dto, Long userId) {
        //1、根据userId和bizId查找唯一的点赞记录
        LikedRecord record = lambdaQuery().eq(LikedRecord::getUserId, userId)
                .eq(LikedRecord::getBizId, dto.getBizId())
                .one();
        //2、记录不为空 代表已经点赞了 不可以再次点赞
        if (record != null) {
            return false;//
        }
        //3、记录为空 新增记录
        LikedRecord likedRecord = LikedRecord.of(null, userId, dto.getBizId(), dto.getBizType(), null, null);
        //4、返回结果
        return save(likedRecord);
    }
}
